import datetime

from sqlalchemy import Column, Integer, String, Text, Boolean, DateTime

from . import base


class Job(base):
    __tablename__ = "jobs"

    id = Column(Integer, primary_key=True, autoincrement=True)  # 作业ID

    name = Column(String(200))  # 作业名
    creator = Column(String(100))  # 创建者
    send_list = Column(String(200)) #
    cc_list = Column(String(200))   #

    aps_type = Column(String(10))  # 调度方式  date/cron/interval
    aps_cfg = Column(Text)  # 调度配置
    start_date = Column(DateTime)   # 开始时间
    end_date = Column(DateTime, nullable=True)  # 结束时间

    step = Column(Text)  # 作业配置

    is_delete = Column(Boolean, default=False)  # 作业是否被删除
    is_unique = Column(Boolean, default=False)  # 作业是否运行独占节点
    type = Column(String(10))   # job type
    tag = Column(String(100))

    create_time = Column(DateTime, default=datetime.datetime.now)  # 创建时间


class TriggeredTask(base):
    __tablename__ = "Tasks"

    id = Column(Integer, primary_key=True, autoincrement=True)
    job_id = Column(Integer)
    trigger_index = Column(Integer)
    trigger_time = Column(DateTime, default=datetime.datetime.now)


class FinishedTask(base):
    """"""
    __tablename__ = "FinishedTasks"

    id = Column(Integer, primary_key=True, autoincrement=True)
    job_id = Column(Integer)
    trigger_index = Column(Integer)
    start_time = Column(DateTime)
    end_time = Column(DateTime)
    result = Column(String(50))


class DelayedTask(base):
    """因独占推迟(取消)的任务"""
    __tablename__ = "DelayedTasks"

    id = Column(Integer, primary_key=True, autoincrement=True)
    job_id = Column(Integer)
    trigger_index = Column(Integer)
    delayed_time = Column(DateTime, default=datetime.datetime.now)


class RebootStep(base):
    """记录重启的步骤"""
    __tablename__ = "RebootSteps"

    id = Column(Integer, primary_key=True, autoincrement=True)
    job_id = Column(Integer)
    trigger_index = Column(Integer)
    step_index = Column(Integer)
    is_success = Column(Boolean, default=True)
    is_deal = Column(Boolean, default=False)


class StepResult(base):
    __tablename__ = "StepResults"

    id = Column(Integer, primary_key=True, autoincrement=True)
    job_id = Column(Integer)
    trigger_index = Column(Integer)
    step_index = Column(Integer)
    start_time = Column(DateTime)
    end_time = Column(DateTime)
    result = Column(String(50))
